import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/commodity/categories/page/searchCommodity/search_commodity_provider.dart';
import 'package:boss/ui/page/commodity/categories/widget/categories_search_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'search_commodity_history.dart';
import 'search_commodity_list.dart';

class SearchCommodityBrowser extends StatefulWidget {
  @override
  _SearchCommodityBrowserState createState() => _SearchCommodityBrowserState();
}

class _SearchCommodityBrowserState extends State<SearchCommodityBrowser> {
  TextEditingController _searchController;

  @override
  void initState() {
    super.initState();
    _searchController = TextEditingController();
  }

  @override
  void dispose() {
    _searchController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            children: <Widget>[
              Expanded(
                child: CategoriesSearchBar(
                  controller: _searchController,
                  onChanged: (value) {
                    var provider = Provider.of<SearchCommodityProvider>(
                      context,
                      listen: false,
                    );
                    provider.setSearchWord(value);
                  },
                ),
              ),
              Container(
                margin: EdgeInsets.only(
                  right: 24.w,
                ),
                child: LabelButton(
                  title: '取消',
                  textStyle: TextStyleConstant().normal_1_28,
                  onTap: () {
                    var provider = Provider.of<SearchCommodityProvider>(
                      context,
                      listen: false,
                    );
                    provider.setSearchWord('');

                    _searchController.text = '';
                  },
                ),
              ),
            ],
          ),
          Consumer<SearchCommodityProvider>(
            builder: (context, provider, _) {
              return provider.searchWord.length > 0 ? SearchCommodityList() : SearchCommodityHistory();
            },
          ),
        ],
      ),
    );
  }
}
